package com.ebt.m.customer.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.adapter.CheckCompanyAdapter;
import com.ebt.m.customer.adapter.CheckRiskAdapter;
import com.ebt.m.customer.entity.CompanyBean;
import com.ebt.m.customer.entity.RiskBean;
import com.ebt.m.customer.net.http.PolicyHttps;
import com.ebt.m.customer.net.json.ErrorJson;
import com.ebt.m.customer.net.json.PolicyAllJson;
import com.ebt.m.data.provider.WikiProvider;
import com.ebt.m.utils.UIHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择公司或主险
 * Created by archer.qi on 2016/12/8.
 */
public class CheckCompanyOrRiskActivity extends Activity implements View.OnClickListener {

    public static final String EXTRA_PAGE = "Page";
    public static final String EXTRA_ID = "ID";
    public static final String EXTRA_PID = "PID";
    public static final String EXTRA_DATA_FOR_RETURN = "DataForReturn";

    private TextView mTvTitle;
    private TextView mTvComplete;
    private ImageView mIvBack;

    private GridView mGridView;
    private ListView mListView;
    private TextView mTvEmpty;

    private List<CompanyBean> gridData = new ArrayList<>();
    private CheckCompanyAdapter mGridAdapter;

    private List<RiskBean> listData = new ArrayList<>();
    private CheckRiskAdapter mListAdapter;

    /* 0->选择品牌， 1->选择主险 */
    private int Page = -1;
    private String companyId;
    /* 当前选中的Company或Risk的ID*/
    private String mIntentId;

    /*暂时一次请求所有*/
    private static final int PAGE_SIZE = Integer.MAX_VALUE;
    /*暂时一页请求所有数据*/
    private int pageIndex = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company_or_risk_check);
        setupViews();
    }

    private void setupViews() {
        mTvTitle = (TextView) findViewById(R.id.top_title);
        mTvComplete = (TextView) findViewById(R.id.top_complete);
        mIvBack = (ImageView) findViewById(R.id.top_back);

        mTvEmpty = (TextView) findViewById(R.id.empty);

        mGridView = (GridView) findViewById(R.id.grid_company);
        mListView = (ListView) findViewById(R.id.list_risk);

        mIvBack.setOnClickListener(this);
        mTvComplete.setOnClickListener(this);

        setOnItemClickListener();
        getExtras();
        switchPage();

        requestData();
    }

    private void requestData() {
        WikiProvider provider = new WikiProvider(CheckCompanyOrRiskActivity.this);
        if (Page == 0) {
            getBrandList();
        } else if (Page == 1) {
            listData = provider.getRisksById(companyId);
            if (listData != null && listData.size() > 0) {
                setAdapter();
            } else {
                setEmpty();
            }
        }
    }

    private void getExtras() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            Page = bundle.getInt(EXTRA_PAGE);
            if (bundle.containsKey(EXTRA_ID)) {
                companyId = bundle.getString(EXTRA_ID);
            }
            mIntentId = bundle.getString(EXTRA_PID);
        }
    }

    private void getBrandList() {
        PolicyHttps.getInstance(this.getApplicationContext()).getCompanyList(pageIndex, PAGE_SIZE, new PolicyHttps.onResponseCallback() {
            @Override
            public void onResponse(PolicyAllJson data) {
                if (data != null) {
                    gridData = data.companyList;
                    if (gridData != null && gridData.size() > 0) {
                        setAdapter();
                    } else {
                        setEmpty();
                    }
                }
            }

            @Override
            public void onError(ErrorJson error) {
                UIHelper.makeToast(CheckCompanyOrRiskActivity.this, getString(R.string.network_fail));
            }

            @Override
            public void onFailure(Throwable t) {
                UIHelper.makeToast(CheckCompanyOrRiskActivity.this, getString(R.string.network_fail));
            }
        });
    }

    private void setOnItemClickListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mListAdapter.setCheckedPosition(i);
            }
        });
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mGridAdapter.setCheckedPosition(i);
            }
        });
    }

    private void setAdapter() {
        if (Page == 0) {
            mGridAdapter = new CheckCompanyAdapter(this, gridData);
            if (!TextUtils.isEmpty(mIntentId)) {
                mGridAdapter.setCheckId(mIntentId);
            }
            mGridView.setAdapter(mGridAdapter);
        } else if (Page == 1) {
            mListAdapter = new CheckRiskAdapter(this, listData);
            if (!TextUtils.isEmpty(mIntentId)) {
                mListAdapter.setCheckId(mIntentId);
            }
            mListView.setAdapter(mListAdapter);
        }
    }

    /**
     * 切换页面
     */
    private void switchPage() {
        if (Page == 0) {
            mTvTitle.setText("选择品牌");
            mGridView.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);
        } else if (Page == 1) {
            mTvTitle.setText("选择主约");
            mGridView.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);
        }
    }

    private void setEmpty() {
        if (Page == 0) {
            mGridView.setVisibility(View.GONE);
            mTvEmpty.setText("没有品牌");
        } else if (Page == 1) {
            mListView.setVisibility(View.GONE);
            mTvEmpty.setText("没有主约");

        }
        mTvEmpty.setVisibility(View.VISIBLE);
    }

    /**
     * 完成
     */
    private void onComplete() {
        if (Page == 0) {
            // 品牌
            if (mGridAdapter != null) {
                CompanyBean company = mGridAdapter.getItemSelected();
                Intent intent = getIntent();
                Bundle bundle = new Bundle();
                bundle.putSerializable(EXTRA_DATA_FOR_RETURN, company);
                bundle.putSerializable(EXTRA_PAGE, Page);
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
            }
        } else if (Page == 1) {
            // 主险
            if (mListAdapter != null) {
                RiskBean risk = mListAdapter.getItemSelected();
                Intent intent = getIntent();
                Bundle bundle = new Bundle();
                bundle.putSerializable(EXTRA_DATA_FOR_RETURN, risk);
                bundle.putSerializable(EXTRA_PAGE, Page);
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
            }
        }
        finish();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_back:
                finish();
                break;
            case R.id.top_complete:
                onComplete();
                break;
        }
    }
}
